<template>
  <header>
    <div class="user_avant">
      <van-image
        round
        width="5rem"
        height="5rem"
        :src="userInfo.avatar?userInfo.avatar:'http://yanxuan.nosdn.127.net/8945ae63d940cc42406c3f67019c5cb6.png'"
      />
    </div>
    <div class="user_login">
      <span v-if="isUserStatus">{{userInfo.username?userInfo.username:''}}</span>
      <van-icon name="arrow" style="float:right" />
      <p @click="showLogin(isUserStatus)">{{userInfo.username?'注销':'点击登录'}}</p>
    </div>
  </header>
</template>

<script>
export default {
  data() {
    return {
      // 用户信息
      userInfo: {},
      isUserStatus: false
    };
  },
  methods: {
    showLogin(isUserStatus) {
      if (isUserStatus) {
        localStorage.clear();
        this.$toast.success("注销成功");
        window.location.reload();
      } else {
        this.$store.commit("changehideLogin", true);
      }
    }
  },
  created() {
    // 如果是空对象直接return不执行下面代码
    if (JSON.parse(localStorage.getItem("userInfo")) === null) {
      this.userInfo = {};
    } else {
      // 取数据
      this.userInfo = JSON.parse(localStorage.getItem("userInfo"));
      // 状态改成true
      this.isUserStatus = true;
    }
  }
};
</script>

<style lang="less" scope>
header {
  padding: 30px 2%;
  display: flex;
  justify-content: space-between;
  align-items: center;
  background: #333;
  .user_avant {
    padding-right: 10px;
  }
  .user_login {
    color: #fff;
    flex: 1;
    margin-top: -20px;
    span,
    i {
      font-size: 20px;
    }
    p {
      margin: 0;
      padding-top: 5px;
    }
  }
}
</style>